<?php
namespace  app\super\controller;
use think\Db;

class CommentController extends  CommonController {

    public function getCommentList(){
        $where = '';
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['keyword']!=''){
            $keyword = $_POST['keyword'];
            $where['ul.life_name|u.user_username'] = array('like',"%$keyword%");
        }

        if($_POST['start']!=''){//时间
            $where['a.create_time'] =array('>=',$_POST['start']);
        }
        if($_POST['end']!=''){//时间
            $where['a.create_time'] =array('<=',$_POST['end']);
        }

        $res = Db::table('up_discuss')->alias('a')
            ->join('up_u_user u','u.user_id = a.user_id','left')
            ->join('up_life ul','ul.life_id = a.life_id','left')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->field('u.user_username,a.*,ul.life_name')
            ->order('a.create_time DESC')
            ->select();
        $total = Db::table('up_discuss')->alias('a')
            ->join('up_u_user u','u.user_id = a.user_id','left')
            ->join('up_life ul','ul.life_id = a.life_id','left')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /***
     * 删除评论信息
     */
    public function delConfirmation(){
        $cid = $_POST['cid'];
        if($cid!=''){
            Db::startTrans();//开启事物
            $where['discuss_id'] = $cid;
            $res =  Db::table('up_discuss')->where($where)->delete();
            if($res){
                Db::commit();//提交事物
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            }else{
                Db::rollback();//回滚
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
            return json_encode($returnData);
        }
    }

    /**
     * 生活分类列表
     */
    public function getClassList(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['keyword']!=''){
            $keyword = $_POST['keyword'];
            $where['region_name'] = array('like',"%$keyword%");
        }
        $where['region_fid'] = 'ZDvaTNytyem97oNtRh692WpwYW8C3YPr';
        $res = Db::table('up_dic_region')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table('up_dic_region')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    /**
     * 删除分类
     */
    public function delClass(){
        $cid = $_POST['cid'];
        if($cid!=''){
            Db::startTrans();//开启事物
            $where['region_id'] = $cid;
            $res =  Db::table('up_dic_region')->where($where)->delete();
            if($res){
                Db::commit();//提交事物
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            }else{
                Db::rollback();//回滚
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
            return json_encode($returnData);
        }
    }
    /**
     * 添加分类页面
     */
    public function addClass(){
        return $this->fetch('life/addClass');
    }

    /***
     * 添加操作
     */
    public function insertClass(){
        if(trim($_POST['region_name'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "生活分类不能为空";
            return json_encode($returnData);
        }
        $data['region_fid'] = 'ZDvaTNytyem97oNtRh692WpwYW8C3YPr';
        $data['region_name'] = $_POST['region_name'];
        $data['region_id'] = $this->getRandChar(32);
        $res = Db::table('up_dic_region')->insert($data);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "添加成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "添加失败";
            return json_encode($returnData);
        }
    }

    /**
     * 编辑页面
     */
    public function updateComment(){
        $nid = $_POST['nid'];
        $res = Db::table('up_dic_region')->where('region_id',$nid)->find();
        $this->assign('res',$res);
        return $this->fetch('life/updateComment');
    }

    /**
     * 编辑功能
     */
    public function updateCommentInfo(){
        $region_id = $_POST['region_id'];
        $region_name = $_POST['region_name'];
        $res = Db::table('up_dic_region')->where('region_id',$region_id)->setField('region_name',$region_name);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "编辑成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "编辑失败";
            return json_encode($returnData);
        }
    }

}
